package com.kedou.sdk.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.kedou.sdk.assist.Constant;
import com.kedou.sdk.params.bean.GameNoticeModel;
import com.kedou.sdk.params.bean.PopupConfig;
import com.kedou.sdk.params.bean.WindowMessage;
import com.kedou.sdk.ui.activity.FloatExtensionBarActivity;
import com.kedou.sdk.ui.assist.GameNoticeListAdapter;
import com.kedou.sdk.ui.assist.OnFinishActivityListener;

import java.util.ArrayList;
import java.util.List;

public class GameNoticeFragment extends BaseDialogFragment implements AdapterView.OnItemClickListener, View.OnClickListener {

    private List<GameNoticeModel> gameNoticeModels= new ArrayList<>();

    public static final GameNoticeFragment newInstance(Bundle bundle) {
        GameNoticeFragment fragment = new GameNoticeFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    private ListView titleListView;
    private ImageView closeBtn;
    private Button contactBtn;
    private TextView titleText;
    private GameNoticeListAdapter mListAdapter;
    private WebView contentWebview;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
//        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//
//        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//        setCancelable(false);
        Bundle bundle = getArguments();
        if(bundle.containsKey("notice")){
            gameNoticeModels = bundle.getParcelableArrayList("notice");
        }
        int noticeType = 0;
        if(bundle.containsKey("popupConfig")){
            PopupConfig popupConfig = bundle.getParcelable("popupConfig");
            noticeType = popupConfig.getNoticeUIType();
        }
        View contentView = doInflateContentView(inflater, container,
                noticeType==1?
                getLayoutIdentifier("hnssn_fragment_game_notice"):
                        getLayoutIdentifier("hnssn_fragment_game_notice_chuanqi"));
        titleListView = contentView.findViewById(getViewIdentifier("gamenotice_listview"));
        closeBtn = contentView.findViewById(getViewIdentifier("gamenotice_close"));
        closeBtn.setOnClickListener(this);
        contactBtn = contentView.findViewById(getViewIdentifier("gamenotice_button"));
        contactBtn.setOnClickListener(this);
        titleText = contentView.findViewById(getViewIdentifier("gamenotice_title"));
        titleText.setOnClickListener(this);
        contentWebview = contentView.findViewById(getViewIdentifier("gamenotice_content"));
        contentWebview.setBackgroundColor(0);
        contentWebview.getBackground().setAlpha(0);
        contentWebview.setWebViewClient(new WebViewClient(){
                                            @Override
                                            public void onPageFinished(WebView view, String url) {
                                                super.onPageFinished(view, url);
                                                view.scrollTo(0,0);
                                            }
                                        }
        );
        titleListView = contentView.findViewById(getViewIdentifier("gamenotice_listview"));
        titleListView.setOnItemClickListener(this);
        mListAdapter = new GameNoticeListAdapter(noticeType);
        mListAdapter.setAdapterData(gameNoticeModels);
        mListAdapter.setLastSelectedIndex(0);
        titleListView.setAdapter(mListAdapter);
        setData();
        return contentView;
    }

    private void setData(){
        GameNoticeModel modle = mListAdapter.getSelectItem();
        if(modle!=null){
            titleText.setText(modle.getTitle());

            contentWebview.loadDataWithBaseURL(null, modle.getContent(), "text/html", "utf-8", null);
            if(modle.getKFBTSwitch()!=1){
//                ((LinearLayout)contactBtn.getParent()).setVisibility(View.GONE);
                contactBtn.setVisibility(View.GONE);
            }else {
                contactBtn.setVisibility(View.VISIBLE);
//                ((LinearLayout)contactBtn.getParent()).setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mListAdapter.setLastSelectedIndex(position);
        setData();
    }

    @Override
    public void onClick(View v) {
        if(v.equals(closeBtn)){
            OnFinishActivityListener listener = getOnFinishActivityListener();
            if (listener != null) {
                listener.doFinishActivity(this);
            }
        }else if(v.equals(titleText)){
            GameNoticeModel modle = mListAdapter.getSelectItem();


            Intent intent = new Intent(getActivity(), FloatExtensionBarActivity.class);
            WindowMessage message = new WindowMessage();
            message.setJumpType(modle.getJumpToTab().equals("service")?"customer":modle.getJumpToTab());
            if(!TextUtils.isEmpty(modle.getExtraField())){
                message.setAppendString("&"+modle.getExtraField()+"="+modle.getArticleId());
            }
            intent.setPackage(getActivity().getPackageName());
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(Constant.LocalIntentExtra.INTENT_FLOAT_WINDOW_DATA,message);
            getActivity().startActivity(intent);
        }
        else if (v.equals(contactBtn)){
            GameNoticeModel modle = mListAdapter.getSelectItem();


            Intent intent = new Intent(getActivity(), FloatExtensionBarActivity.class);
            WindowMessage message = new WindowMessage();
            message.setJumpType(modle.getJumpToTab().equals("service")?"customer":modle.getJumpToTab());
            intent.setPackage(getActivity().getPackageName());
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(Constant.LocalIntentExtra.INTENT_FLOAT_WINDOW_DATA,message);
            getActivity().startActivity(intent);

        }
    }

    private OnFinishActivityListener getOnFinishActivityListener() {
        if (!isNullContext()) {
            FragmentActivity activity = getActivity();
            if (activity instanceof OnFinishActivityListener) {
                return (OnFinishActivityListener) activity;
            }
        }
        return null;
    }
}
